from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from django_redis import get_redis_connection
from rest_framework.permissions import IsAuthenticated
from rest_framework.viewsets import ModelViewSet

from goods.models import SKU
<<<<<<< HEAD
from rest_framework.generics import ListAPIView,UpdateAPIView
from .models import OrderInfo, OrderGoods
from .serializers import OrderSettlementSerializer,OrderCommitSerializer,OrderCommentSerializer,OrderEvaluationSerializer
from decimal import Decimal
from rest_framework.generics import CreateAPIView
from rest_framework.decorators import action
=======
from mall import settings
from orders.models import OrderInfo, OrderGoods
from .serializers import OrderSettlementSerializer,OrderCommitSerializer, OrderListSerializer
# OrderListSerializer
from decimal import Decimal
from rest_framework.generics import CreateAPIView, ListAPIView
>>>>>>> 8555cb73645f7a4db53de205c2658d7ef3c47cb1

"""
订单结算
1, 当前端在购物车页面点击去结算的时候
2, 取出redis中的,选中的商品,结算
3, 返回对应的结果

"""
class OrderSettlementView(APIView):

    permission_classes = [IsAuthenticated]

    def get(self,request):

        #0,获取用户对象
        user = request.user

        #1,获取redis中的数据
        redis_conn = get_redis_connection("cart")
        redis_cart = redis_conn.hgetall("cart_%s"%user.id)
        redis_cart_selected = redis_conn.smembers("cart_selected_%s"%user.id)

        #2,通过商品编号,取出商品对象
        sku_list = []
        for sku_id in redis_cart_selected:
            #2,1取出商品对象
            sku = SKU.objects.get(id=sku_id)

            #2,2设置商品数量
            sku.count = redis_cart[sku_id]

            #2,3,添加到列表中
            sku_list.append(sku)

        #3,创建序列化器,转换数据格式
        data = {
            "freight":Decimal("10.0"),
            "skus":sku_list
        }
        serializer =  OrderSettlementSerializer(data)
        return Response(serializer.data)


"""
提交订单(生成订单)
1, 当前端点击提交订单的时候
2, 携带参数
3, 校验参数,创建order订单,和ordergoods数据
"""
class OrderCommitView(CreateAPIView):
    permission_classes = [IsAuthenticated]
    serializer_class = OrderCommitSerializer

<<<<<<< HEAD
class CommentGoodsView(ListAPIView):
    parser_classes = [IsAuthenticated]
    serializer_class = OrderCommentSerializer

    def get_queryset(self):
        order_id = self.kwargs['order_id']
        order = OrderInfo.objects.get(order_id=order_id)
        return order.skus.filter(is_commented=0)

class EvaluationCommentGoodsView(UpdateAPIView):
    """
    订单评论
    """
    parser_classes = [IsAuthenticated]
    serializer_class = OrderEvaluationSerializer

    def get_object(self):
        sku_id = self.request.data.get('sku')
        order_id = self.kwargs['order_id']
        # print(OrderGoods.objects.get(order_id=order_id,sku_id=sku_id))
        return OrderGoods.objects.get(order_id=order_id,sku_id=sku_id)
=======


#获取订单列表
class OrderListViewSet(ListAPIView):
    permission_classes = [IsAuthenticated]
    serializer_class = OrderListSerializer

    def get_queryset(self):
        #获取用户
        user=self.request.user
        #返回用户订单信息
        return user.orderinfo_set.all()

    # def get(self, request, *args, **kwargs):
    #
    #     return request
    # def get(self,request):
    #     # 获取用户
    #     user = request.user
    #     #获取订单
    #     orderinfo=OrderInfo.objects.filter(user=user.id)
    #     return orderinfo
    #
    #
    # # def get(self,request):
    # #     pass
    #
    #     # #查询该用户的订单信息
    #     # order_ids = []
    #     # orders_list=OrderInfo.objects.filter(user=user.id)
    #     # #获取该用户的订单号列表
    #     # for order in orders_list:
    #     #     order_ids.append(order.order_id)
    #     #
    #     # #获取该用户的订单详情对象列表
    #     # skus = []
    #     # for order_id in order_ids:
    #     #     ordergoods=OrderGoods.objects.filter(order=order_id)
    #     #     print(ordergoods)
    #     #
    #     #     for ordergood in ordergoods:
    #     #         print(ordergood.sku.id)
    #     #         sku=SKU.objects.get(pk=ordergood.sku.id)
    #     #         skus.append(sku)
    #     #     print(skus[0].price)
    #     #         # print(ordergood.sku)
    #
    #     # orders_list=OrderGoods.objects.filter(user=user.id)
    #     # print(orders_list)
    #     # for order in orders_list:
    #     #     orders.append(order)
    #     #     print(orders[0])
    #
    #     # for order in orders_list:
    #     #     orders.append(order)
    #     #     print(orders)
    #     #     for order_detail in orders:
    #     #         print(order_detail.status)
    #     #     order_details=OrderGoods.objects.filter(order=order.order_id)
    #     #     for order_detail in order_details:
    #     #         sku_id=order_detail.sku.id
    #     #         print(sku_id)
    #     # orders=orders_list
    #     # serializer=OrderListSerializer(data=skus,many=True)
    #     # serializer.is_valid(raise_exception=True)
    #     #
    #     # #根据用户的订单信息查询订单商品详情
    #     # return Response(serializer.validated_data,status=200)
>>>>>>> 8555cb73645f7a4db53de205c2658d7ef3c47cb1
